// 区间dp题目
// 以一个区间作为考虑
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 310, INF = 1e9;
int n;
int s[N];
int dp[N][N];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        s[i] += s[i - 1];
    }
    // 区间dp问题需要从小区间往大区间进行状态转移
    for (int len = 2; len <= n; ++len)
        for (int i = 1; i + len - 1 <= n; ++i)
        {
            int l = i, r = i + len - 1;
            int sum = s[r] - s[l - 1];
            dp[l][r] = INF;
            for (int j = i; j < r; ++j)
                dp[l][r] = min(dp[l][r], dp[l][j] + dp[j + 1][r] + sum);
        }
    cout << dp[1][n] << endl;
    return 0;
}